package com.yjs.app.trade.core.util;

import java.util.HashSet;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

//import org.apache.log4j.Logger;


import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;

/**
 * @author hikaru
 * Class Explain:JedisUtil  
 */
public class JedisUtil { 
	
	private static JedisPool jedisPool = null;
	
	private static JedisSentinelPool redisSentinel = null;
	
	private static Logger logger = LoggerFactory.getLogger(Logger.class);
	
	static {
		setJedisInfo();
	}
	
	public static void setJedisInfo () {
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxTotal(Integer.parseInt(PropertiesUtils.getProperty("redis.pool.maxTotal").toString()));
		config.setMaxIdle(Integer.parseInt(PropertiesUtils.getProperty("redis.pool.maxIdle").toString()));
		config.setTestOnBorrow(Boolean.getBoolean(PropertiesUtils.getProperty("redis.pool.testOnBorrow").toString()));
		config.setTestOnReturn(Boolean.getBoolean(PropertiesUtils.getProperty("redis.pool.testOnReturn").toString()));
		config.setTestWhileIdle(Boolean.getBoolean(PropertiesUtils.getProperty("redis.pool.testWhileIdle").toString()));
		config.setMaxWaitMillis(Integer.parseInt(PropertiesUtils.getProperty("redis.pool.maxWaitMillis").toString()));
		try {
			Set<String> sentinels = new HashSet<>();
			String redisUrls [] = PropertiesUtils.getProperty("redis.server.host").split(",");
			for (int i = 0; i < redisUrls.length; i++) {
				sentinels.add(redisUrls[i]);
			}
			redisSentinel = new JedisSentinelPool(PropertiesUtils.getProperty("redis.server.master").toString(), sentinels, config, 
					Integer.parseInt(PropertiesUtils.getProperty("redis.server.timeout").toString()));
		} catch (Exception e) {
			redisSentinel = null;
		}
		try {
			jedisPool = new JedisPool(config, PropertiesUtils.getProperty("redis.server.host").toString().split(",")[0], 
					Integer.parseInt(PropertiesUtils.getProperty("redis.server.port").toString()),
					Integer.parseInt(PropertiesUtils.getProperty("redis.server.timeout").toString()), null);
		} catch (Exception e) {
			jedisPool = null;
		}
	}
	
	public synchronized static Jedis getJedis() {
		Jedis jedis = null;
		try {
			if (redisSentinel != null) {
				jedis = redisSentinel.getResource();
			} 
			if (jedisPool != null) {
				jedis = jedisPool.getResource();
			}
		} catch (Exception e) {
			logger.error("redis连接出错" + e);
		}
		return jedis;
	}
	
	private static void closeResource (Jedis jedis) {
		if (jedis != null) {
			jedis.close();
		}
	}
	
	public static void set (String key, String value) {
		Jedis jedis = getJedis();
		try {
			jedis.set(key, value);
		} catch (Exception e) {
			logger.error("redis出错:" + e);
			setJedisInfo();
		} finally {
			closeResource(jedis);
		}
	}
	
	public static void setex (String key, String value, int seconds) {
		Jedis jedis = getJedis();
		try {
			jedis.setex(key, seconds,value);
		} catch (Exception e) {
			logger.error("redis出错:" + e);
			setJedisInfo();
		} finally {
			closeResource(jedis);
		}
	}
	
	public static String get (String key) {
		Jedis jedis = getJedis();
		try {
			return jedis.get(key);
		} catch (Exception e) {
			logger.error("redis出错:" + e);
			setJedisInfo();
		} finally {
			closeResource(jedis);
		}
		return null;
	}
	
	public static void deleteKey (String key) {
		Jedis jedis = getJedis();
		try {
			jedis.del(key);
		} catch (Exception e) {
			logger.error("redis出错:" + e);
			setJedisInfo();
		} finally {
			closeResource(jedis);
		}
	}
	
	public static void main(String[] args) {
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxIdle(256);
		config.setTestOnBorrow(true);
		config.setTestOnReturn(true);
		config.setTestWhileIdle(true);
		config.setMinEvictableIdleTimeMillis(600001);
		config.setTimeBetweenEvictionRunsMillis(30001);
		config.setNumTestsPerEvictionRun(-1);
		Set<String> sentinels = new HashSet<>();
		sentinels.add("192.168.13.30:16379");
		sentinels.add("192.168.13.30:26379");
		JedisSentinelPool redisSentinel = new JedisSentinelPool("yjs-cache-cluster", sentinels, config, 60000);
		/*jedisPool = new JedisPool(config,"192.168.13.30",16379,60000, null);
		jedisPool.getResource().set("key", "xxxxxx");
		System.out.println(jedisPool.getResource().get("org.user.context.c09745093ee94f54a5d8d5027959f578385892688637384"));*/
		Jedis jedis = null;
		jedis = redisSentinel.getResource();
		System.out.println(jedis.get("aaa"));
		System.out.println(jedis.get("test"));
		redisSentinel.close();
	}
}
